const api = require('../../utils/request.js');

Page({
  data: {
    orderNo: '',
    order: null,
    loading: true
  },

  onLoad(options) {
    if (options.orderNo) {
      this.setData({
        orderNo: options.orderNo
      });
      this.loadOrderDetail();
    }
  },

  async loadOrderDetail() {
    try {
      wx.showLoading({
        title: '加载中...'
      });

      const result = await api.getOrderDetail(this.data.orderNo);
      
      if (result.code === 200 && result.data) {
        const order = result.data;
        order.createTime = this.formatTime(order.createTime);
        order.paymentTime = order.paymentTime ? this.formatTime(order.paymentTime) : '';
        order.statusText = this.getStatusText(order.status);
        order.paymentMethodText = this.getPaymentMethodText(order.paymentMethod);
        
        this.setData({
          order: order,
          loading: false
        });
      }
    } catch (error) {
      console.error('加载订单详情失败:', error);
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
      this.setData({
        loading: false
      });
    } finally {
      wx.hideLoading();
    }
  },

  async handlePay() {
    if (this.data.order.status !== 1) {
      wx.showToast({
        title: '订单状态不正确',
        icon: 'none'
      });
      return;
    }

    try {
      wx.showLoading({
        title: '支付中...'
      });

      const result = await api.payOrder(this.data.orderNo);
      
      wx.hideLoading();
      
      if (result.code === 200) {
        wx.showToast({
          title: '支付成功',
          icon: 'success'
        });
        
        // 重新加载订单详情
        setTimeout(() => {
          this.loadOrderDetail();
        }, 1500);
      }
    } catch (error) {
      wx.hideLoading();
      console.error('支付失败:', error);
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      });
    }
  },

  async handleCancel() {
    wx.showModal({
      title: '确认取消',
      content: '确定要取消这个订单吗？',
      success: async (res) => {
        if (res.confirm) {
          try {
            wx.showLoading({
              title: '取消中...'
            });

            const result = await api.cancelOrder(this.data.orderNo);
            
            wx.hideLoading();
            
            if (result.code === 200) {
              wx.showToast({
                title: '订单已取消',
                icon: 'success'
              });
              
              // 重新加载订单详情
              setTimeout(() => {
                this.loadOrderDetail();
              }, 1500);
            }
          } catch (error) {
            wx.hideLoading();
            console.error('取消订单失败:', error);
            wx.showToast({
              title: '取消失败',
              icon: 'none'
            });
          }
        }
      }
    });
  },

  formatTime(timeStr) {
    if (!timeStr) return '';
    const date = new Date(timeStr);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}`;
  },

  getStatusText(status) {
    const statusMap = {
      1: '待支付',
      2: '已支付',
      3: '已完成',
      4: '已取消'
    };
    return statusMap[status] || '未知';
  },

  getPaymentMethodText(method) {
    const methodMap = {
      1: '现金',
      2: '微信',
      3: '支付宝'
    };
    return methodMap[method] || '未知';
  }
});

